﻿#pragma once
#include "qaccountmanagement.h"
#include "ui_qaccountmanagement.h"
#include "qlogin.h"
#include "qmpsw.h"
#include "qaddaccount.h"
#include <QDebug>
#include <QMessageBox>
extern std::map <QString,QString> UserInfo;
extern std::map <QString,QString> Usertype;//账户标志

//思路
//1.先数据整理，筛选出管理员账户和普通用户账户
//2.初始化列表，填入管理员账户和普通用户信息
//3.点击列表进行信息读取然后进行按钮操作，改变UserInfo中信息，调用写文件函数更新

QAccountmanagement::QAccountmanagement(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::QAccountmanagement)
{
    ui->setupUi(this);
    init_dialog();
}

QAccountmanagement::~QAccountmanagement()
{
    delete ui;
}

void QAccountmanagement::init_dialog(){
    Init_UserInfoRead();
    Init_Skin();
    Init_Control();
    Init_SConnect();
    //Init_UserInfoRead();
}

void QAccountmanagement::Init_Skin(){
    this->setWindowTitle(QString::fromLocal8Bit("账户管理"));
    this->setAttribute (Qt::WA_DeleteOnClose);
    setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint| Qt::WindowCloseButtonHint |Qt::WindowMinimizeButtonHint|Qt::WindowMaximizeButtonHint| Qt::WindowTitleHint );

}

void QAccountmanagement::Init_Control(){
    //列表控件初始化
    Init_table();

}

void QAccountmanagement::Init_SConnect(){

}

void QAccountmanagement::Init_UserInfoRead(){
    for (std::map<QString,QString>::iterator it = Usertype.begin(); it!=Usertype.end(); ++it)
    {
        QString str1 = it->second;
        if(str1 == "1"){
            //管理员账户
            Adminaccount[it->first] = UserInfo[it->first];
        }else if(str1 == "2"){
            //工程师账户
            engineeraccount[it->first] = UserInfo[it->first];

        }else if (str1 == "3") {
            //操作员账户
            operatoraccount[it->first] = UserInfo[it->first];
        }
    }
}

void QAccountmanagement::Init_table(){
    QStringList sListHeader;
    ui->tableWidget->setColumnCount(3);
    sListHeader<<QString::fromLocal8Bit("账户名")<<QString::fromLocal8Bit("密码")<<QString::fromLocal8Bit("账户类型");
    ui->tableWidget->setHorizontalHeaderLabels(sListHeader);
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//每列大小一致
    ui->tableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);//根据内容调整行高度
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); //设置选择模式，选择单行
    ui->tableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);//禁止鼠标拖动缩放行高度
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); //设置不可编辑
    ui->tableWidget->horizontalHeader()->setStretchLastSection(true);//最后一项占用所有剩下的空间
    ui->tableWidget->verticalHeader()->setVisible(false);//垂直标题设置为不可见
    // ui->tableWidget->setTextElideMode()setEchoMode
    data_import();
}

void QAccountmanagement::data_import(){
    ui->tableWidget->clearContents();
    for(int i = ui->tableWidget->rowCount();i > 0;i--)
    {
        ui->tableWidget->removeRow(0);
    }
    for (std::map<QString,QString>::iterator it = Adminaccount.begin(); it!=Adminaccount.end(); ++it)
    {
        QTableWidgetItem *item;
        int iRow = ui->tableWidget->rowCount();
        ui->tableWidget->insertRow(iRow);//加一行
        item = new QTableWidgetItem(it->first);
        item->setTextAlignment(Qt::AlignCenter);
        ui->tableWidget->setItem(iRow, 0, item);
        item = new QTableWidgetItem(it->second);
        item->setTextAlignment(Qt::AlignCenter);
        ui->tableWidget->setItem(iRow, 1, item);
        item = new QTableWidgetItem(QString::fromLocal8Bit("管理员"));
        item->setTextAlignment(Qt::AlignCenter);
        ui->tableWidget->setItem(iRow, 2, item);
    }

    for (std::map<QString,QString>::iterator it = engineeraccount.begin(); it!=engineeraccount.end(); ++it)
    {
        QTableWidgetItem *item;
        int iRow = ui->tableWidget->rowCount();
        ui->tableWidget->insertRow(iRow);//加一行
        item = new QTableWidgetItem(it->first);
        item->setTextAlignment(Qt::AlignCenter);
        ui->tableWidget->setItem(iRow, 0, item);
        item = new QTableWidgetItem(it->second);
        item->setTextAlignment(Qt::AlignCenter);
        ui->tableWidget->setItem(iRow, 1, item);
        item = new QTableWidgetItem(QString::fromLocal8Bit("工程师"));
        item->setTextAlignment(Qt::AlignCenter);
        ui->tableWidget->setItem(iRow, 2, item);
    }
    for (std::map<QString,QString>::iterator it = operatoraccount.begin(); it!=operatoraccount.end(); ++it)
    {
        QTableWidgetItem *item;
        int iRow = ui->tableWidget->rowCount();
        ui->tableWidget->insertRow(iRow);//加一行
        item = new QTableWidgetItem(it->first);
        item->setTextAlignment(Qt::AlignCenter);
        ui->tableWidget->setItem(iRow, 0, item);
        item = new QTableWidgetItem(it->second);
        item->setTextAlignment(Qt::AlignCenter);
        ui->tableWidget->setItem(iRow, 1, item);
        item = new QTableWidgetItem(QString::fromLocal8Bit("操作员"));
        item->setTextAlignment(Qt::AlignCenter);
        ui->tableWidget->setItem(iRow, 2, item);
    }
}

void QAccountmanagement::on_pB_addaccount_clicked()
{
    QAddaccount *window_Addaccount = new QAddaccount(this);
    int res = window_Addaccount->exec();
    if(res == QDialog::Accepted)
    {
        qDebug()<<QString::fromLocal8Bit("增加一个账户");
        Init_UserInfoRead();
        data_import();
    }
    else if (res == QDialog::Rejected)
    {
        return;
    }
}

void QAccountmanagement::on_pB_deleteaccount_clicked()
{
    QList<QTableWidgetItem*> items = ui->tableWidget->selectedItems();
    int count = items.count();
    if(count == 3){
        int row = ui->tableWidget->row(items.at(0));//获取行

        QString accountname =  items.at(0)->text(); //获取内容
        if(accountname == "admin")
        {
            QMessageBox::warning(this,QString::fromLocal8Bit("警告"),QString::fromLocal8Bit("此账户不可删除"));
            return;
        }
        QString password = items.at(1)->text();

        QString type = items.at(2)->text();
        if(type == QString::fromLocal8Bit("管理员")){
            //删除对应管理员账户
            std::map<QString,QString>::iterator it;
            it = Adminaccount.find(accountname);
            Adminaccount.erase (it);

        }
        else if(type == QString::fromLocal8Bit("工程师")){
            std::map<QString,QString>::iterator it;
            it = engineeraccount.find(accountname);
            engineeraccount.erase (it);
        }
        else if(type == QString::fromLocal8Bit("操作员")){
            //删除对应普通员账户
            std::map<QString,QString>::iterator it;
            it = operatoraccount.find(accountname);
            operatoraccount.erase (it);
        }
        //删除UserInfo，Usertype中对应账户
        std::map<QString,QString>::iterator it;
        it = UserInfo.find(accountname);
        UserInfo.erase (it);

        it = Usertype.find(accountname);
        Usertype.erase (it);
    }else {

    }
    data_import();

}

void QAccountmanagement::on_pB_resetpassword_clicked()
{
    Qmpsw *window_password = new Qmpsw(this);
    int res = window_password->exec();
    if(res == QDialog::Accepted)
    {
        qDebug()<<QString::fromLocal8Bit("密码更改成功");
        Init_UserInfoRead();
        data_import();
    }
    else if (res == QDialog::Rejected)
    {
        return;
    }
}
